#!/bin/bash
#该脚本需要在root权限下运行
#------------------------------------------------------
#获取该脚本所在地址
MYDIR=$(cd "$(dirname "$0")";pwd)
#获取桌面端还是服务端
FLAG=$1
PACKAGES=$( cat deb-$FLAG.dep )

if [ "${TYPE}" == "server" ] || [ "${TYPE}" == "ts_server" ]; then
  FLAG="server"
elif [ "${TYPE}" == "desktop" ] || [ "${TYPE}" == "ts_desktop" ]; then
  FLAG="desktop"
fi

#安装GCBS所需依赖并打开rpcbind
dependencePackage(){
	#GCBS依赖
	i=0
	if [ -f ${MYDIR}/install_packages.log ]; then
		rm ${MYDIR}/install_packages.log
	fi
	echo `date "+安装日期：%Y-%m-%d时间：%H:%M:%S"` >> "${MYDIR}/install_packages.log"

	for line in $PACKAGES ;do
		((i++))
		PACKAGE=$line
		PACKAGE[$i]=$PACKAGE
		dpkg -s ${PACKAGE[i]}
		ret=$?
		if (( $ret == 1 ));then
			apt install -y ${PACKAGE[i]}
			dpkg -s ${PACKAGE[i]}
			if (( $? == 1 ));then
				apt install -y ${PACKAGE[i]}
				dpkg -s ${PACKAGE[i]}
				if (( $? == 1 ));then
					echo "ERROR: the ${PACKAGE[i]} name is incorrect or requires manual installation" >> "${MYDIR}/install_packages.log"
                                        info=$(sudo apt-get -y install ${PACKAGE[i]})
                                        echo -e "error occur $info \n" >> "${MYDIR}/error_packages.log"

				else
					echo "${PACKAGE[i]}: had installed rightly again" >> "${MYDIR}/install_packages.log"
				fi
			else
				echo "${PACKAGE[i]}: had installed rightly" >> "${MYDIR}/install_packages.log"
			fi
		elif (( $ret == 2 ));then
			echo "ERROR: Incorrect usage: apt install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret == 126 ));then
			echo "ERROR: Not an executable: apt install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret == 127 ));then
			echo "ERROR: Command not found: apt install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret ==  0  ));then
			echo "${PACKAGE[i]}: had installed" >> "${MYDIR}/install_packages.log"
		else
			echo "ERROR: ${PACKAGE[i]}: unknown code[$ret]" >> "${MYDIR}/install_packages.log"
		fi
	done
}

#主函数
EnvPrepare(){	

	#安装GCBS所需依赖
	dependencePackage

	#打开rpcbind
	rpcbind

}

EnvPrepare

